<?php
/**
 * +--------------------------------------------------------+
 * @Created by Phpstorm.
 * @Category StockRepository.php
 * @Depiction
 * @Author hwy <429208948@qq.com>
 * @Date 2022/4/19  17:51
 * +--------------------------------------------------------+
 */

namespace App\Repositories;

use App\Models\Stock;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

class StockRepository extends BaseRepository
{
    protected $stock;

    public function __construct(Stock $stock)
    {
        $this->stock = $stock;
    }

    /**
     * @param $params
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/24  14:58
     */
    public function getList($params)
    {

        $query = $this->stock->query();

        $perPage = $this->perPage($params);
        [$sort, $order] = $this->paramsSet($params);

        if (!empty($params['title'])) {
            $query = $query->where('content', 'like', '%' . $params['title'] . '%')
                ->orWhere('title', 'like', '%' . $params['title'] . '%');
        }

        return $list = $query->orderBy($sort, $order)->paginate($perPage);

    }

    /**
     * @return bool
     * @Depiction
     * @Author hwy <429208948@qq.com>
     * @Date 2022/4/24  16:07
     */
    public function addData($stock_list)
    {
        if (!empty($stock_list)){
            $this->stock->query()->truncate();
            $this->stock->query()->insert($stock_list);
        }

        return true;
    }




}
